package com.forsir.android.mhfinance.storeObjects;

import android.content.ContentValues;
import android.database.Cursor;

import com.forsir.android.mhfinance.helper.DbAdapter;

public abstract class IStorable {
	private long Id = -1;

	public void setId(long id) {
		Id = id;
	}

	public long getId() {
		return Id;
	}

	public void loadFromDb(DbAdapter dbHelper) {
		final Cursor cursor = dbHelper.fetchEntry(this);
		fillFromCursor(cursor);
		cursor.close();
	}

	public abstract String getTableName();

	public abstract ContentValues getContent();

	public abstract String[] getColumns();

	public abstract String getName();

	/**
	 * gets values from actual position of cursor
	 */
	public abstract IStorable fillFromCursor(Cursor cursor);

	public boolean save(DbAdapter dbHelper) {
		boolean result = true;
		if (getId() > 0) {
			result = result && dbHelper.updateEntry(this);
		} else {
			result = result && dbHelper.createEntry(this) > 0;
		}
		return result;
	}
}
